<?php
if(!empty($_POST)){
	include ("xlsuatinbai.php");
}else{
define("ROOT_PATH",str_replace("admin","",dirname($_SERVER['PHP_SELF'])));
include('../fckeditor/fckeditor.php'); 

$sids = $_SESSION["ArraySec"];
$aid = $_GET["aid"];

$sql = "select * from tblarticle where id = $aid";
$rs = mysql_query($sql);
if(mysql_num_rows($rs)==1){
	$article = mysql_fetch_object($rs);
}
 if($_SESSION['Permit']==1)
$sql = "select id,name from tblsection";
else
$sql = "select id,name from tblsection where id in ($sids)";
$rs = mysql_query($sql);
$slcSection = "<select id='slcSection' name='sid'>";
while($row=mysql_fetch_array($rs)){
	if($row["id"]==$article->section_id){
		$slcSection.="<option value='". $row["id"]."' selected='selected'>".$row["name"]."</option>";
	}else{
		$slcSection.="<option value='". $row["id"]."'>".$row["name"]."</option>";
	}  
}
?>
<script type="text/javascript" src="<?php echo ROOT_PATH."ckfinder/ckfinder.js"?>">
</script>
<script language="javascript" type="text/javascript">

function editArticle(){
	var frm = document.frmEditArticle;
	var sid = document.getElementsByName("sid").item(0).value;
	var vi_title = frm.vi_title.value;
	var vi_summary = frm.vi_summary.value;
	var vi_content = frm.vi_content.value;
	var en_title = frm.en_title.value;
	var en_summary = frm.en_summary.value;
	var en_content = frm.en_content.value;
	var image = frm.image.value;
	var tag = frm.tag.value;
	
	var msg ="";
	if(sid == 0){
		msg+="Hãy chọn một chuyên mục cho bài viết!\n";
	}
	if(vi_title ==""){
		msg+="Thiếu tiêu đề Tiếng Việt!\n";
	}
	if(vi_summary == ""){
		msg+="Thiếu tóm tắt Tiếng Việt!\n";
	}
//	if(vi_content==""){
//		msg+="Thiếu nội dung Tiếng Việt!\n";
//	}

	if(en_title ==""){
		msg+="Thiếu tiêu đề Tiếng Anh !\n";
	}
	if(en_summary == ""){
		msg+="Thiếu tóm tắt Tiếng Anh!\n";
	}
//	if(en_content==""){
//		msg+="Thiếu nội dung Tiếng Anh!\n"
//	}
	
	if(tag==""){
		msg+="Thiếu từ khóa hỗ trợ tìm kiếm!\n"
	}
	
	if(image==""){
		msg+="Thiếu nội dung hình ảnh đại diện!\n"
	}
	
	if(msg.length==0){
		frm.submit();
	}else{
		alert(msg);
	}
}
function BrowseServer()
{

	// You can use the "CKFinder" class to render CKFinder in a page:
	var finder = new CKFinder() ;
	finder.basePath = '<?php echo "ckfinder/";?>' ;	// The path for the installation of CKFinder (default = "/ckfinder/").
	
	finder.selectActionFunction = function(fileUrl){
		
		document.getElementById( 'image' ).value = fileUrl ;
		document.getElementById( 'img' ).src = fileUrl;
		
	}
	finder.popup() ;

	
}
</script>

<form action=""  method="post" name="frmEditArticle">
<table style="width:100%" border="0" align="center" >
  <tr align="center">
    <td colspan="2" style="background:#008000;color:white; height:30px;"><h2>Sửa bài viết</h2></td>
  </tr>
  <tr>
    <td ><strong>Chuyên mục</strong>:</td>
    <td><?php echo $slcSection;?></td>
  </tr>
  <tr>
    <td width="30%" align="left"><strong>Xuất bản</strong></td>
    <td>
    	
    	<input  type="radio" name="published" value="1" <?php echo ($article->published==1)?"checked='checked'":""?>/><label>Có</label>
        <input  type="radio" name="published" value="0" <?php echo ($article->published==0)?"checked='checked'":""?>/><label>Không</label>
    </td>
  </tr>
  <tr>
  		<td><strong>Hình ảnh</strong></td>
  		<td>
  			<input type="text" id="image" name="image" size="60" value="<?php echo $article->image?>"></input>
  			<input type="button" value="Chọn hình ảnh" onclick="BrowseServer();"/>
  		</td>
  </tr>
  <tr>
  	<td>&nbsp;</td>
  	<td><img border="1" src="<?php echo $article->image;?>" height="100" id="img"/></td>
  </tr>
  <tr>
  	<td><strong>Từ khóa cho bài viết</strong></td>
  	<td><input type="text" name="tag" size="60" value="<?php echo $article->tag;?>"/></td>
  </tr>
  <tr>
  	<th colspan="2"  style="background:#e7e7e7; height:25px;">Nội dung Tiếng Việt</th>
  </tr>
 
  <tr>
    <td width="30%"><strong>Tiêu đề</strong></td>
    <td><input size="60" type="text" name="vi_title" value="<?php echo $article->vi_title?>"/></td>
  </tr>

  <tr>
    <td width="30%"><strong>Tóm tắt</strong></td>
    <td><textarea name="vi_summary" style="width:400px; height:100px;"><?php echo $article->vi_summary;?></textarea></td>
  </tr>
  
 
  <tr>
    <td> <strong>Nội dung</strong></td>
  </tr>
    
    <tr>
    	<td colspan="2">
        <?php
			// Automatically calculates the editor base path based on the _samples directory.
			// This is usefull only for these samples. A real application should use something like this:
			// $oFCKeditor->BasePath = '/fckeditor/' ;	// '/fckeditor/' is the index value.

			$sBasePath = ROOT_PATH.'fckeditor/' ;
			
			$oFCKeditor = new FCKeditor('vi_content') ;
			$oFCKeditor->BasePath	= $sBasePath ;
			$oFCKeditor->Value		= $article->vi_content;
			$oFCKeditor->Width		= 700;
			$oFCKeditor->Height		= 400;
			$oFCKeditor->Create() ;
		?>
        </td>
    </tr>
   <tr>
  	<th colspan="2"  style="background:#e7e7e7; height:25px;">Nội dung Tiếng Anh</th>
  </tr>
   	
  <tr>
    <td width="30%"><strong>Tiêu đề</strong></td>
    <td><input size="60" type="text" name="en_title" value="<?php echo $article->en_title;?>"/></td>
  </tr>

  <tr>
    <td width="30%"><strong>Tóm tắt</strong></td>
    <td><textarea name="en_summary" style="width:400px; height:100px;"><?php echo $article->en_summary;?></textarea></td>
  </tr>
  
 
  <tr>
    <td> <strong>Nội dung</strong></td>
  </tr>
    
    <tr>
    	<td colspan="2">
        <?php
			// Automatically calculates the editor base path based on the _samples directory.
			// This is usefull only for these samples. A real application should use something like this:
			// $oFCKeditor->BasePath = '/fckeditor/' ;	// '/fckeditor/' is the index value.

			$sBasePath = ROOT_PATH.' fckeditor/' ;
			
			$oFCKeditor = new FCKeditor('en_content') ;
			$oFCKeditor->BasePath	= $sBasePath ;
			$oFCKeditor->Value		= $article->en_content;
			$oFCKeditor->Width		= 700;
			$oFCKeditor->Height		= 400;
			$oFCKeditor->Create() ;
		?>
        </td>
    </tr>
  
  <tr align="center">
    <td colspan="2">
    	<input  type="button" name="btnSave" value="Lưu" onclick="editArticle();"/>
        <input  type="button" name="btnReset" value="Làm lại" />
        </td>
    </tr>
</table>
<input type="hidden" name="id" value="<?php echo $article->id?>"/>
</form>

<?php }?>